package com.xiaoyu.lanling.feature.user.view

import `in`.srain.cube.util.LocalDisplay
import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.feature.view.StatusLayout
import com.xiaoyu.lanling.media.image.ImageLoadParam
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.view_album.*
import kotlinx.android.synthetic.main.view_album.view.*

class AlbumFragment : OnShowFragment() {

    companion object {
        val sOnClickListener = View.OnClickListener { v ->
            val url = v.getViewTag<String>(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_URL)
                    ?: return@OnClickListener
            val photos = v.getViewTag<ArrayList<String>>(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_URL_LIST)
                    ?: return@OnClickListener
            val activity = App.getInstance().topActivity ?: return@OnClickListener
            Router.instance.gotoPhotoActivity(activity, url, photos)
        }

        fun create(url: String, photos: ArrayList<String>): OnShowFragment {
            val fragment = AlbumFragment()

            val params = Bundle()
            params.putString(Constant.PARAM_URL, url)
            params.putStringArrayList(Constant.IntentKey.KEY_PHOTO_IMAGE_URL_LIST, photos)
            fragment.arguments = params
            return fragment
        }
    }

    private val width = LocalDisplay.SCREEN_WIDTH_PIXELS
    private val height = (width / 1.125F).toInt()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = StatusLayout(AppContext.getContext())
        view.layoutParams = ViewGroup.LayoutParams(width, height)

        return view
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)
        (view as? StatusLayout)?.showLazyViewWithLoading(LayoutInflater.from(context), R.layout.view_album, 0)
        arguments?.let { args ->
            val url = args.getString(Constant.PARAM_URL)
            val photos = args.getStringArrayList(Constant.IntentKey.KEY_PHOTO_IMAGE_URL_LIST)
            val loadParam = ImageLoadParam.newBuilder()
                    .setUrl(url)
                    .setEnableEmptyUrl(true)
                    .setWidth(width)
                    .setHeight(height)
                    .setOverlayResId(R.color.user_info_avatar_cover)
                    .setBitmapConfig(Bitmap.Config.ARGB_8888)
                    .build()

            ImageViewUtil.loadImage(album_view, loadParam)
            album_view.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_URL, url)
            album_view.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_URL_LIST, photos)
        }
    }
}
